//index.js
//获取应用实例
var app = getApp(),
  a = app.requirejs("core"),
  b = app.requirejs('api/kjb');
Page({
  data: {
    status: ["正在进行", "即将开始", "全部活动"],
    kj_type: 1,
    cate_info: [],
    page: 1,
    cate_index: -1,
    show: false,
    loaded: false,
    list: []
  },

  onLoad: function (e) {
    a.setting();
    var that = this;
    that.setData(e);
    that.getList();
    b.BarIndex(function (t) {
      t.cate_info.forEach(function (t, k) {
        if (t.id == e.cate_id) {
          that.setData({
            cate_index: k
          })
        }
      })
      that.setData(t)
    })

  },
  getList: function () {
    var that = this,
      page = that.data.page,
      kj_type = that.data.kj_type,
      cate_id = that.data.cate_id;
    wx.setNavigationBarTitle({
      title: that.data.class_name || "活动列表"
    })
    b.kj_list(cate_id, kj_type, page, that, function (t) {
      that.setData(t)
    })
  },
  /**
   * 状态转换
   */
  status: function (e) {
    var kj_type = parseInt(e.detail.value) + 1;
    this.setData({
      kj_type: kj_type,
      list: [],
      page: 1,
      loaded: false
    })
    this.getList()
  },
  /**
   * 分类转换
   */
  cate: function (e) {
    var that=this,
    cate_index = e.detail.value;
    that.data.cate_info.forEach(function (t, k) { 
      if (k == cate_index) {
      that.setData({
        cate_index: cate_index,
        cate_id: t.id,
        list: [],
        page: 1,
        loaded: false,
        class_name: t.class_name
      })
      that.getList()
      }
    })
    
  },
  /**
  * 下拉刷新
  */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false
    })
    this.getList()
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    this.data.loaded || this.getList();
  },
  /**
   * 跳转至详情
   */
  url: function (e) {
    var data = a.pdata(e);
    if (data.goods_type == 2) {
      wx.navigateTo({
        url: '/yb_shop/pages/kanjia/goods_info/index?id=' + data.id
      })
    }
  },
  /** 
   * 用户点击右上角分享 
   */
  onShareAppMessage: function () {

  }
})
